
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.MalformedURLException;

import javax.xml.parsers.ParserConfigurationException;

import map.NodeDataBase;
import map.NodeSlot;
import map.model.APIServer;

import org.xml.sax.SAXException;

public class Downloader {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws SAXException 
	 * @throws ParserConfigurationException 
	 * @throws MalformedURLException 
	 */
	public static void main(String[] args) throws MalformedURLException, ParserConfigurationException, SAXException, IOException {
		APIServer server = new APIServer("http://api.openstreetmap.org",new File("cache"));
		map.model.Map map = server.readMap(new Rectangle2D.Double(17,47,1,1));
		//map.model.Map map = server.loadMapFromOSMFile(new File("/home/balage/download/map.osm"));
		PrintWriter log = new PrintWriter(new File("map.ndb.log"));
		
		NodeDataBase ndb = NodeDataBase.create(map);
		int nodes = 0;
		int splitlats = 0;
		int splitlons = 0;
		int lists = 0;
		int sections = 0;
		for(int i = 0;i<ndb.nodes.size();i++){
			NodeSlot ns = ndb.nodes.get(i);
			log.println(i+" -> "+ns);
			switch(ns.type){
			case NodeSlot.TYPE_NODE: nodes++; break;
			case NodeSlot.TYPE_SPLIT_LAT: splitlats++; break;
			case NodeSlot.TYPE_SPLIT_LON: splitlons++; break;
			case NodeSlot.TYPE_LIST: lists++; break;
			case NodeSlot.TYPE_WAY: sections++; break;
			}
		}
		log.close();
		System.out.println("Nodes: "+nodes);
		System.out.println("Splits: "+splitlats+" by lat "+splitlons+" by lon.");
		System.out.println("Lists: "+lists);
		System.out.println("Way sections: "+sections);
		System.out.println("Saving..");
		ndb.save(new File("map.ndb"));
		
		
	}

}
